/*
 * @Author: shenya shenya0203@163.com
 * @Date: 2024-02-02 11:04:43
 * @LastEditors: shenya shenya0203@163.com
 * @LastEditTime: 2024-04-18 15:14:42
 * @FilePath: /code/include/mqtt_cli.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef __MQTT_CLI_H__
#define __MQTT_CLI_H__

#include "mosquitto.h"
#include <stdint.h>
#include <semaphore.h>
#include <sys/types.h>

#define NUM_OF_AREA 7
#define NUM_OF_INFO 8

enum HZ_Info {
    LIST = 0,
    DETAIL = 1,
    STATUS,
    MODIFY,
    CONTROL,
    LOG,
    ENERGY,
    EVENT,
};

enum HZ_Area {
    PROJECT = 0,
    SPACE = 1,
    REGION,
    GROUP,
    SCENE,
    DEVICER,
    ALL,
};

typedef struct
{
    char project[8];
    char space[6];
    char region[7];
    char group[6];
    char scene[6];
    char device[7];
    char all[4];
} hz_area_t;

typedef struct
{
    char list[5];
    char detail[7];
    char status[7];
    char modify[7];
    char control[8];
    char log[4];
    char energy[7];
    char event[6];
} hz_info_t;

extern hz_area_t hz_area;
extern hz_info_t hz_info;

extern struct mosquitto *g_test_mosq;
struct mosquitto *connect_to_mqtt_server(char *server_ip);
int mqtt_bcast_msg(char *api, char *data, int len);
int mqtt_send_msg(char *info, char *area, char *data, int len);
int mqtt_send_json(char *info, char *area, bool status, cJSON *data_json);

void mqtt_publish_test();

// 定义消息结构体
#define MSG_BUFFER_SIZE 100
struct msg_buffer {
    long msg_type;
    uint16_t  msg_textLen;
    uint8_t msg_text[MSG_BUFFER_SIZE];
};

extern sem_t reportDevState_limitSem;
extern sem_t reportDevState_syncSem;
extern bool reportDevState_syncSem_initialized;
extern struct msg_buffer message_recv;
extern key_t msg_key;
extern int msgid;
void report_sem_init();
uint8_t report_sem_post();
uint8_t report_sem_trywait();
uint8_t OneDevTriggerDataReport(uint16_t short_addr);
uint8_t OneDevReportResult_wait();
uint8_t reportMsg_send(uint8_t *data, uint16_t dataLen);
uint8_t reportMsg_nowaitRecv(uint8_t **data, uint16_t *dataLen);

void controlResult_Sem_init();
int controlResult_Sem_post();
int controlResult_Sem_timedwait();
int controlResult_Sem_signal();
int controlResult_cond_wait();

void mqttMsg_hz_user_get(cJSON *json);
void mqttMsg_devices_screen_rhy_info(cJSON *json);
void reportMsg_create();

int read_rhythmData(char *buffer);
#endif
